package model

import (
	"encoding/json"
	"fmt"
	"ota/model/entity"
)

type UpgradePlanCreateRequest struct {
	Id         int64           `json:"id"`
	StrategyId int64           `json:"strategy_id"`
	Name       string          `json:"name"`
	TargetType int             `json:"target_type"`
	QueryCond  json.RawMessage `json:"query_cond"`
	StartTime  int64           `json:"start_time"`
	Overseer   string          `json:"overseer"`
	Tester     string          `json:"tester"`
	Inspector  string          `json:"inspector"`
	CreatedAt  int64           `json:"created_at"`
	UpdatedAt  int64           `json:"updated_at"`
}

func (a *UpgradePlanCreateRequest) Check() error {
	if a.StrategyId == 0 {
		return fmt.Errorf("strategy_id is invalid")
	}

	if a.Name == "" {
		return fmt.Errorf("name is invalid")
	}

	if a.TargetType == 0 {
		return fmt.Errorf("target_type is invalid")
	}

	if a.QueryCond == nil {
		return fmt.Errorf("query_cond is invalid")
	}

	if a.Overseer == "" {
		return fmt.Errorf("overseer is invalid")
	}
	if a.Tester == "" {
		return fmt.Errorf("tester is invalid")
	}

	return nil
}

type UpgradePlanUpdateRequest struct {
	Id         *int64          `json:"id"`
	StrategyId *int64          `json:"strategy_id"`
	Name       *string         `json:"name"`
	TargetType *int            `json:"target_type"`
	QueryCond  json.RawMessage `json:"query_cond"`
	Detail     json.RawMessage `json:"detail"`
	StartTime  *int64          `json:"start_time"`
	Status     *int            `json:"status"`
	Overseer   *string         `json:"overseer"`
	Tester     *string         `json:"tester"`
	Inspector  *string         `json:"inspector"`
	CreatedAt  *int64          `json:"created_at"`
	UpdatedAt  *int64          `json:"updated_at"`
}

func (a *UpgradePlanUpdateRequest) Check() error {

	if !(a.Id != nil && *a.Id != 0) {
		return fmt.Errorf("id is invalid")
	}

	if !(a.StrategyId != nil && *a.StrategyId != 0) {
		return fmt.Errorf("strategy_id is invalid")
	}

	if !(a.Name != nil && *a.Name != "") {
		return fmt.Errorf("name is invalid")
	}

	if !(a.TargetType != nil && *a.TargetType != 0) {
		return fmt.Errorf("target_type is invalid")
	}

	if !(a.QueryCond != nil && len(a.QueryCond) != 0) {
		return fmt.Errorf("query_cond is invalid")
	}

	if !(a.Overseer != nil && *a.Overseer != "") {
		return fmt.Errorf("overseer is invalid")
	}
	if !(a.Tester != nil && *a.Tester != "") {
		return fmt.Errorf("tester is invalid")
	}

	return nil
}

type UpgradePlanListRequest struct {
	Id         *int64          `json:"id"`
	StrategyId *int64          `json:"strategy_id"`
	Name       *string         `json:"name"`
	TargetType *int            `json:"target_type"`
	StartTime  *int64          `json:"start_time"`
	Status     *int            `json:"status"`
	QueryCond  json.RawMessage `json:"query_cond"`
	Overseer   *string         `json:"overseer"`
	Tester     *string         `json:"tester"`
	Inspector  *string         `json:"inspector"`
	CreatedAt  *int64          `json:"created_at"`
	UpdatedAt  *int64          `json:"updated_at"`
	Index      int             `json:"index"`
	Size       int             `json:"size"`
}

type UpgradePlanListResponse struct {
	Total int                `json:"total"`
	List  []*UpgradePlanInfo `json:"list"`
}

type UpgradePlanInfoRequest struct {
	Id int64 `json:"id"`
}
type UpgradePlanInfo struct {
	Id                  int64                   `json:"id"`
	StrategyId          int64                   `json:"strategy_id"`
	UpgradeStrategyInfo *entity.UpgradeStrategy `json:"upgrade_strategy_info"`
	Name                string                  `json:"name"`
	QueryCond           json.RawMessage         `json:"query_cond"`
	Detail              json.RawMessage         `json:"detail"`
	TargetType          int                     `json:"target_type"`
	UpgradeType         int                     `json:"upgrade_type"`
	StartTime           int64                   `json:"start_time"`
	CountDetail         string                  `json:"count_detail"`
	FailureCount        int                     `json:"failure_count"`
	SuccessCount        int                     `json:"success_count"`
	TotalCount          int                     `json:"total_count"`
	PrepareCount        int                     `json:"prepare_count"`
	UpgradingCount      int                     `json:"upgrading_count"`
	CancelCount         int                     `json:"cancel_count"`
	Status              int                     `json:"status"`
	Overseer            string                  `json:"overseer"`
	Tester              string                  `json:"tester"`
	Inspector           string                  `json:"inspector"`
	Operator            int64                   `json:"operator"`
	OperatorName        string                  `json:"operator_name"`
	CreatedAt           int64                   `json:"created_at"`
	UpdatedAt           int64                   `json:"updated_at"`
}

type UpgradePlanDeleteRequest struct {
	Id int64 `json:"id"`
}

type UpgradePlanTerminalRequest struct {
	Id int64 `json:"id"`
}

type UpgradePlanListUpgradeObjectRequest struct {
	Id            int64   `json:"id"`
	TargetType    int     `json:"target_type"`
	UserId        int64   `json:"user_id"`
	UserName      string  `json:"user_name"`
	UserPhone     string  `json:"user_phone"`
	EbikeId       int64   `json:"ebike_id"`
	EbikeName     string  `json:"ebike_name"`
	EbikeSnCode   *string `json:"ebike_sn_code"`
	EbikeTypeId   *int64  `json:"ebike_type_id"`
	EbikeTypeName *string `json:"ebike_type_name"`
	CreatedAt     int64   `json:"created_at"`
	UpdatedAt     int64   `json:"updated_at"`
	Index         int     `json:"index"`
	Size          int     `json:"size"`
}

type UpgradePlanListUpgradeObjectResponse struct {
	Total int         `json:"total"`
	List  interface{} `json:"list"`
}

type CheckQueryCondType struct {
	TargetType int             `json:"target_type"`
	QueryCond  json.RawMessage `json:"query_cond"`
}

type ListUpgradeObjectRequest struct {
	Id int64 `json:"id"`
}
